// utils/weather.js
const config = require('./config.js')

class WeatherAPI {
  constructor() {
    this.key = config.heweather.key
    this.baseUrl = config.heweather.baseUrl
    this.geoBaseUrl = config.heweather.geoBaseUrl
  }

  // 获取城市位置信息
  async getLocation(cityName = '北京') {
    try {
      const response = await new Promise((resolve, reject) => {
        wx.request({
          url: `${this.geoBaseUrl}/city/lookup`,
          data: {
            key: this.key,
            location: cityName,
            range: 'cn',
            number: 1
          },
          success: resolve,
          fail: reject
        })
      })
      
      if (response.statusCode === 200 && response.data.code === '200') {
        return response.data.location[0]
      }
      throw new Error('获取位置信息失败')
    } catch (error) {
      console.error('获取位置信息错误:', error)
      throw error
    }
  }

  // 获取实时天气
  async getCurrentWeather(locationId) {
    try {
      const response = await new Promise((resolve, reject) => {
        wx.request({
          url: `${this.baseUrl}/weather/now`,
          data: {
            key: this.key,
            location: locationId
          },
          success: resolve,
          fail: reject
        })
      })
      
      if (response.statusCode === 200 && response.data.code === '200') {
        return response.data.now
      }
      throw new Error('获取天气信息失败')
    } catch (error) {
      console.error('获取天气信息错误:', error)
      throw error
    }
  }

  // 获取空气质量
  async getAirQuality(locationId) {
    try {
      const response = await new Promise((resolve, reject) => {
        wx.request({
          url: `${this.baseUrl}/air/now`,
          data: {
            key: this.key,
            location: locationId
          },
          success: resolve,
          fail: reject
        })
      })
      
      if (response.statusCode === 200 && response.data.code === '200') {
        return response.data.now
      }
      throw new Error('获取空气质量失败')
    } catch (error) {
      console.error('获取空气质量错误:', error)
      throw error
    }
  }

  // 获取三日天气预报
  async getThreeDayForecast(locationId) {
    try {
      const response = await new Promise((resolve, reject) => {
        wx.request({
          url: `${this.baseUrl}/weather/3d`,
          data: {
            key: this.key,
            location: locationId
          },
          success: resolve,
          fail: reject
        })
      })
      
      if (response.statusCode === 200 && response.data.code === '200') {
        return response.data.daily
      }
      throw new Error('获取天气预报失败')
    } catch (error) {
      console.error('获取天气预报错误:', error)
      throw error
    }
  }

  // 获取生活指数
  async getLivingIndices(locationId, type = '1,3,9') {
    try {
      const response = await new Promise((resolve, reject) => {
        wx.request({
          url: `${this.baseUrl}/indices/1d`,
          data: {
            key: this.key,
            location: locationId,
            type: type // 1: 运动, 3: 洗车, 9: 穿衣
          },
          success: resolve,
          fail: reject
        })
      })
      
      if (response.statusCode === 200 && response.data.code === '200') {
        return response.data.daily
      }
      throw new Error('获取生活指数失败')
    } catch (error) {
      console.error('获取生活指数错误:', error)
      throw error
    }
  }
}

module.exports = new WeatherAPI()